package gui.reports.supply;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import gui.common.*;
import hit.core.HomeInventoryTracker;
import hit.reports.ExpiredItemsReport;
import hit.reports.HTMLBuilder;
import hit.reports.NMonthSupplyReport;
import hit.reports.PDFBuilder;
import hit.reports.ReportBuilder;

/**
 * Controller class for the N-month supply report view.
 */
	public class SupplyReportController extends Controller implements
		ISupplyReportController {
/*---	STUDENT-INCLUDE-BEGIN */

		private static final int MIN_MONTH = 1;
		private static final int MAX_MONTH = 100;
		
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the N-month supply report view
	 */	
	public SupplyReportController(IView view) {
		super(view);
		
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected ISupplyReportView getView() {
		return (ISupplyReportView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {

		getView().setMonths("3");
	}

	//
	// IExpiredReportController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * N-month supply report view is changed by the user.
	 */
	@Override
	public void valuesChanged() {
		try
		{
			int month = Integer.parseInt(getView().getMonths());
			getView().enableOK((month >= MIN_MONTH && month <= MAX_MONTH));
		}
		catch (Exception ex)
		{
			getView().enableOK(false);
		}
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the N-month supply report view.
	 */
	@Override
	public void display() {
		
		try
		{
			int months = Integer.parseInt(getView().getMonths());
			if (months < MIN_MONTH || months > MAX_MONTH)
			{
				getView().displayErrorMessage("Months must be between 1 and 100.");
				return;
			}
			
			ReportBuilder builder;
			NMonthSupplyReport designer = new NMonthSupplyReport(months);
			
			FileFormat format = getView().getFormat();
			Date date = new Date();
			String filename = ReportBuilder.PATH + "supply-" + date.getTime(); 
			
			if (format == FileFormat.HTML)
			{
				builder = new HTMLBuilder();
				filename += ".html";
			}
			else if (format == FileFormat.PDF)
			{
				builder = new PDFBuilder();
				filename += ".pdf";
			}
			else
			{
				getView().displayErrorMessage("Unknown Format type.");
				return;
			}
			
			HomeInventoryTracker hitCore = HomeInventoryTracker.Instance();
			hitCore.Accept(designer);
			hitCore.AcceptPost(designer);
			hitCore.GetProductIndex().Accept(designer);
			hitCore.GetItemIndex().Accept(designer);
			
			designer.BuildReport(builder);
			
			(new File(ReportBuilder.PATH)).mkdirs();
			File file = new File(filename);
			
			if (format == FileFormat.HTML)
			{
				((HTMLBuilder)builder).GetOutput(file);
			}
			else if (format == FileFormat.PDF)
			{
				((PDFBuilder)builder).GetOutput(file);
			}
			
			java.awt.Desktop.getDesktop().open(file);
		}
		catch(Exception ex)
		{
			getView().displayErrorMessage("Error attempting to create report.");
		}
	}

}

